<?php

/**
 * listvalues actions.
 *
 * @package    sf_sandbox
 * @subpackage listvalues
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class listvaluesActions extends autolistvaluesActions
{
    public function executeMoveup()
    {
        /* @var $obj ListValues */
        $id = (int)$this->getRequestParameter('id', 0);
        $obj = ListValuesPeer::retrieveByPk($id);
        $this->forward404Unless($obj);
        $c = new Criteria();
        $c->add(ListValuesPeer::PARENT_ID, $obj->getParentId());
        APriority::moveUp($c, $id);
        $this->redirect('listvalues/list?filters[parent_id]=' . $obj->getParentId() . '&filter=filter');
    }
    
    public function executeDisable()
    {
        $id = (int)$this->getRequestParameter('id', 0);
        //debugvar('Id: ' . $id);
        $obj = ListValuesPeer::retrieveByPk($id);
        $this->forward404Unless($obj);

        $obj->setStatusId(MyConstants::STATUS_DELETED);
        $obj->save();
        if($this->getRequestParameter('page'))
        {
        	$this->redirect('listvalues/list?filters[parent_id]=' . $obj->getParentId(). '&filter=filter&page=' . $this->getRequestParameter('page'));
        }
        $this->redirect('listvalues/list?filters[parent_id]=' . $obj->getParentId(). '&filter=filter' );
    }
    public function executeEnable()
    {
        $id = (int)$this->getRequestParameter('id', 0);
        //debugvar('Id: ' . $id);
        $obj = ListValuesPeer::retrieveByPk($id);
        $this->forward404Unless($obj);

        $obj->setStatusId(MyConstants::STATUS_APPROVED);
        $obj->save();
    if($this->getRequestParameter('page'))
        {
            $this->redirect('listvalues/list?filters[parent_id]=' . $obj->getParentId(). '&filter=filter&page=' . $this->getRequestParameter('page'));
        }
        $this->redirect('listvalues/list?filters[parent_id]=' . $obj->getParentId(). '&filter=filter');
    }
    
	public function executeGetchildren() 
	{
	    $id = (int)$this->getRequestParameter('id', 0);
	    $c = new Criteria();
	    $c->add(ListValuesPeer::PARENT_ID, $id);
	    $c->add(ListValuesPeer::STATUS_ID, MyConstants::STATUS_DELETED, Criteria::NOT_EQUAL);
        $c->addDescendingOrderByColumn(ListValuesPeer::PRIORITY);
        $c->addAscendingOrderByColumn(ListValuesPeer::VALUE);
	    $obj = ListValuesPeer::doSelect($c);
	    $this->children = array();
        if( !empty($obj) )
        {
            foreach($obj as $ob)
            {
                $this->children[$ob->getId()] = $ob;
            }
        }
        
        //$this->children = ListValues::getByParentId($id);
	}
	
    public function executeEdit()
    {
//        debugvar($this->filters);
//        $this->list_values = $this->getListValuesOrCreate();
//    
//        if ($this->getRequest()->getMethod() == sfRequest::POST)
//        {
//            debugvar($filters = $this->getRequestParameter('filters'));
//            $this->updateListValuesFromRequest();
//            
//            $this->saveListValues($this->list_values);
//            
//            $this->setFlash('notice', 'Your modifications have been saved');
//            
//            if ($this->getRequestParameter('save_and_add'))
//            {
//            return $this->redirect('listvalues/create');
//            }
//            else if ($this->getRequestParameter('save_and_list'))
//            {
//            return $this->redirect('listvalues/list');
//            }
//            else
//            {
//            return $this->redirect('listvalues/edit?id='.$this->list_values->getId());
//            }
//        }
//        else
//        {
//            $this->labels = $this->getLabels();
//        }
        parent::executeEdit();
        $this->filters = $this->getUser()->getAttributeHolder()->getAll('sf_admin/list_values/filters');
        if( !$this->list_values->getParentId() )
        {
            if(isset($this->filters['parent_id']))
            {
                $this->list_values->setParentId($this->filters['parent_id']);
            }
        }
    }
    protected function getListValuesOrCreate($id = 'id')
    {
        $list_values = parent::getListValuesOrCreate($id);
        $list_values->setCulture($this->getUser()->getCulture());
        return $list_values;
    }
    
    public function executeAddbrood()
    {
        if( $this->getRequest()->getMethod() == sfRequest::POST )
        {
            if($this->getRequestParameter('dog[add_brood]', '') )
            {
                $parentId = ListValues::getLabelId('BROODS');
                $value = new ListValues();
                $value->setCulture($this->getUser()->getCulture());
                $value->setName($this->getRequestParameter('dog[add_brood]', ''));
                $value->setStatusId(MyConstants::STATUS_APPROVED);
                $value->setParentId($parentId);
                $value->save();
            }
            $this->dog = $this->getRequestParameter('dog', 0);
        }
    }
}
